new Vue({
    el: '#app',
    data() {
        return {
            fileList: [],
            tableData: [],
            form: {
                page: 1,
                limit: 15,
            },
            loading: false,
            success: 0,
            error: 0,
            city:[],//地址
        }
    },
    mixins:[importMixin],
    mounted(){
        this.getData();
    },
    methods: {
        getData() {
            const that = this;
            $.ajax({
                url: absolutepath() + "utils/city.json",
                type: "GET",
                success: function (data) {
                    that.city = data
                },
                dataType: "json"
            })
        },
        submitUpload(i = 0) {
            if (i < this.tableData.length) {
                this.loading = true;
                if (!this.tableData[i].Supplier_Name) {
                    this.tableData[i].isStatus = '2';
                    this.tableData[i].Cause = '供应商名称不能为空'
                    this.error++;
                    this.submitUpload(i + 1);
                } else if (!this.tableData[i].Tax_Number) {
                    this.tableData[i].isStatus = '2';
                    this.tableData[i].Cause = '纳税识别号不能为空'
                    this.error++;
                    this.submitUpload(i + 1);
                } else if (!this.tableData[i].Supplier_Address) {
                    this.tableData[i].isStatus = '2';
                    this.tableData[i].Cause = '供应商地址不能为空'
                    this.error++;
                    this.submitUpload(i + 1);
                } else if (!this.tableData[i].Bank_Name&&!this.tableData[i].Account_Number) {
                    this.tableData[i].isStatus = '2';
                    this.tableData[i].Cause = '开户行和银行账户不能为空'
                    this.error++;
                    this.submitUpload(i + 1);
                } else { //校验通过
                    let business_supplier_bank_account = []
                    this.tableData[i].Account_Number.split(';').forEach((item,index)=>{
                        business_supplier_bank_account.push({
                            Supplier_Bank_Account_SerialNumber:'',
                            Supplier_SerialNumber:'',
                            Bank_Name:this.tableData[i].Bank_Name.split(';')[index]?this.tableData[i].Bank_Name.split(';')[index]:'',
                            Account_Number:item,
                        })
                    })
                    let obj = {
                        Supplier_SerialNumber: "",
                        Supplier_Name: this.tableData[i].Supplier_Name,
                        Tax_Number: this.tableData[i].Tax_Number,
                        Supplier_Address: this.tableData[i].Supplier_Address,
                        Enterprise_Info_SerialNumber:'',
                        Children:{
                            business_supplier_bank_account
                        }
                    }
                    this.submitForm({ Data: JSON.stringify({ Add: { business_supplier_info: [obj],business_supplier_config:[{Tax_Number:obj.Tax_Number}] } }) }, (res) => {
                        if (res.Status == 'success') {
                            this.tableData[i].isStatus = '1';
                            this.tableData[i].Cause = '',
                            this.success++;
                        } else {
                            this.tableData[i].isStatus = '2';
                            this.tableData[i].Cause = '该供应商已存在（纳税识别号已存在）'
                            this.error++;                  
                        }
                        this.submitUpload(i + 1);
                    })
                }
            } else if (i != 0) {
                this.loading = false;
                this.$message.success("批量导入结束")
            } else {
                this.$message({
                    message: '请先导入模板后再上传',
                    type: 'warning'
                })
            }
        },
        submitForm(data, callback) {
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalSave`,
                type: 'POST',
                data,
                success: function (res) {
                    callback(res)
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            })
        },
        handleChange({ raw }) {//导入监听文件
            const that = this;
            var reader = new FileReader();
            reader.readAsBinaryString(raw);
            reader.onload = function (e) {
                var data = e.target.result;
                var wb = XLSX.read(data, {
                    type: 'binary' //以二进制的方式读取
                });
                var sheet = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
                var json = XLSX.utils.sheet_to_json(sheet);//利用接口实现转换。
                let arr = [];
                json.forEach(item => {
                    arr.push({
                        Supplier_SerialNumber:'',
                        Supplier_Name:(item['供应商名称']+'').trim(),
                        Tax_Number:(item['纳税识别号']+'').trim(),
                        Address:[(item['供应商地址省']+'').trim(),(item['市']+'').trim(),(item['区']+'').trim()],
                        Address_Detail:(item['详细地址']+'').trim(),
                        Supplier_Address:`${(item['供应商地址省']+'').trim()}/${(item['市']+'').trim()}/${(item['区']+'').trim()}/${(item['详细地址']+'').trim()}`,
                        Bank_Name:(item['开户行']+'').trim(),
                        Account_Number:(item['银行账户']+'').trim(),
                        isEdit: false,
                        isStatus: '0',
                        Cause:'',
                    })
                });
                that.tableData = arr
            }
        },
        handleSizeChange(val) {
            this.form.page = 1;
            this.form.limit = val;
        },
        handleCurrentChange(val) {
            this.form.page = val;
        },
        handleEdit(row) {//修改当前行
            row.isEdit = !row.isEdit
        },
        handleRemove(index) {//移除当前行
            this.$confirm('是否要删除此行?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.tableData = this.tableData.filter((i, id) => id != (this.form.page - 1) * this.form.limit + index);
            }).catch(() => { });
        },
        handleClean() {//清空
            this.$confirm('是否要清空导入数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.tableData = []
            }).catch(() => { });
        },
    },
})